﻿using System;
using System.Web.UI.WebControls;
using System.Xml.Linq;

using DotNetNuke.Entities.Modules;

using DesertedRoadStudios.BlogNetNuke.Components;

namespace DesertedRoadStudios.BlogNetNuke.UI
{
    public class CommentForm : PortalModuleBase
    {
        private XDocument _settings;
        private TextBox _tbName;
        private TextBox _tbEmail;
        private TextBox _tbURL;
        private TextBox _tbCommentTitle;
        private TextBox _tbComments;
        private Button _btnBNNSubmit;

        public CommentForm()
        {
        }

        public CommentForm(bool setSettings)
        {
            if (setSettings)
                SetSettings();
        }

        private void SetSettings()
        {
            _settings = (XDocument)Session["BlogNetNuke"];
        }

        protected void Page_Load()
        {
            _tbName = FindControl("tbBNNName") as TextBox;
            if (_tbName == null)
                throw new System.Exception("Control must contain a TextBox control with ID 'tbBNNName'.");
            RequiredFieldValidator rfv = new RequiredFieldValidator();
            rfv.Text = " * Required.";
            rfv.ControlToValidate = "tbBNNName";
            rfv.Display = ValidatorDisplay.Dynamic;
            Controls.AddAt(Controls.IndexOf(_tbName) + 1, rfv);

            _tbEmail = FindControl("tbBNNEmail") as TextBox;
            if (_tbEmail == null)
                throw new System.Exception("Control must contain a TextBox control with ID 'tbBNNEmail'.");
            rfv = new RequiredFieldValidator();
            rfv.Text = " * Required.";
            rfv.ControlToValidate = "tbBNNEmail";
            rfv.Display = ValidatorDisplay.Dynamic;
            Controls.AddAt(Controls.IndexOf(_tbEmail) + 1, rfv);

            _tbURL = FindControl("tbBNNURL") as TextBox;
            if (_tbURL == null)
                throw new System.Exception("Control must contain a TextBox control with ID 'tbBNNURL'.");
            
            _tbCommentTitle = FindControl("tbBNNCommentTitle") as TextBox;
            if (_tbCommentTitle == null)
                throw new System.Exception("Control must contain a TextBox control with ID 'tbBNNCommentTitle'.");
            rfv = new RequiredFieldValidator();
            rfv.Text = " * Required.";
            rfv.ControlToValidate = "tbBNNCommentTitle";
            rfv.Display = ValidatorDisplay.Dynamic;
            Controls.AddAt(Controls.IndexOf(_tbCommentTitle) + 1, rfv);
            
            _tbComments = FindControl("tbBNNComments") as TextBox;
            if (_tbComments == null)
                throw new System.Exception("Control must contain a TextBox control with ID 'tbBNNComments'.");
            rfv = new RequiredFieldValidator();
            rfv.Text = " * Required.";
            rfv.ControlToValidate = "tbBNNComments";
            rfv.Display = ValidatorDisplay.Dynamic;
            Controls.AddAt(Controls.IndexOf(_tbComments) + 1, rfv);

            _btnBNNSubmit = FindControl("btnBNNSubmit") as Button;
            if (_btnBNNSubmit == null)
                throw new System.Exception("Control must contain a Button control with ID 'btnBNNSubmit'.");

            _btnBNNSubmit.UseSubmitBehavior = false;
            _btnBNNSubmit.Click += new EventHandler(_btnBNNSubmit_Click);
        }

        void _btnBNNSubmit_Click(object sender, EventArgs e)
        {
            CommentInfo comment = new CommentInfo()
            {
                CommentID = Guid.NewGuid(),
                PortalID = Convert.ToInt32(_settings.Element("params").Element("Post").Element("TabDetails").Element("PortalID").Value),
                ModuleID = Convert.ToInt32(_settings.Element("params").Element("Post").Element("TabDetails").Element("ModuleID").Value),
                PostID = new Guid(_settings.Element("params").Element("Post").Element("PostDetails").Element("PostID").Value),
                CommentTitle = _tbCommentTitle.Text,
                AuthorName = _tbName.Text,
                Email = _tbEmail.Text,
                IPAddress = Request.UserHostAddress,
                URL = _tbURL.Text,
                CommentDate = DateTime.Now,
                Comments = _tbComments.Text,
                IsApproved = false,
                IsDeleted = false
            };

            BlogNetNukeController bnn = new BlogNetNukeController();
            bnn.AddCommentToPost(comment);
            
            // Temporarily Redirect Until AJAX has been enabled
            Response.Redirect(_settings.Element("params").Element("Post").Element("PostDetails").Element("RelativeURL").Value + "#comment-" + comment.CommentID.ToString().Replace("-", ""));
        }
    }
}